package com.hystrix.api.controller;

import com.hystrix.feign.MemberApiFeign;
import com.hystrix.feign.MemberApiFeignWithHystrix;
import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sound.midi.Soundbank;

@RestController
public class OrderApiController {

    @Autowired
    MemberApiFeign memberApiFeign;

    @Autowired
    MemberApiFeignWithHystrix memberApiFeignWithHystrix;


    @RequestMapping("/getOrder")
    @HystrixCommand(fallbackMethod = "hystrixFallbackMethod")
    /*HystrixCommand默认开启服务降级、服务熔断、服务隔离
    * 服务降级：fallbackMethod中指定的方法
    * 服务熔断：开启服务熔断机制，达到一定阀值时自动开启
    * 服务隔离：以线程池的方法进行隔离，具体可以查看被调用的线程池的名称*/
    public String getOrder(){
        System.out.println("Hystrix线程池名称:"+Thread.currentThread().getName());
        String result = memberApiFeign.getMember();
        System.out.println(result);
        return result;
    }

    public String hystrixFallbackMethod(){
        return "您好，当前系统繁忙，当前服务已被降级，请稍后在尝试！";
    }


    @RequestMapping("/getOrderWithFallback")
    public String getOrderWithFallback(){
        System.out.println("线程池名称:"+Thread.currentThread().getName());
        String result = memberApiFeignWithHystrix.getMember();
        System.out.println(result);
        return result;
    }

    @RequestMapping("/getOrderInfo")
    public String getOrderInfo(){
        System.out.println("线程池名称:"+Thread.currentThread().getName());
        return "返回订单详细信息...";
    }
}
